import $axios from "@/network";

/**
 * 条件查询
 * @returns {*}
 */
export const queryResourcesServiceList = (current, size, dataName, dataType, dataRange, dataDepartment, dateRange) => {
  return $axios({
    method: 'POST',
    url: '/data/dataService/query/resources',
    data: {
      "current": current,
      "size": size,
      "resName": dataName.toString(),
      "resType": dataType.toString(),
      "resRange": dataRange.toString(),
      "resDepartment": dataDepartment.toString(),
      "resDate": dateRange.toString(),
    }
  })
}
/**
 * 条件查询
 * @returns {*}
 */
export const queryResourcesDateBaseList = (current, size, dataName, dataType, dataRange, dataDepartment, dateRange) => {
  return $axios({
    method: 'POST',
    url: '/data/dataBase/query/resources',
    data: {
      "current": current,
      "size": size,
      "resName": dataName.toString(),
      "resType": dataType.toString(),
      "resRange": dataRange.toString(),
      "resDepartment": dataDepartment.toString(),
      "resDate": dateRange.toString(),
    }
  })
}
/**
 * 条件查询
 * @returns {*}
 */
export const queryResourcesDataFileList = (current, size, dataName, dataType, dataRange, dataDepartment, dateRange) => {
  return $axios({
    method: 'POST',
    url: '/data/dataFile/query/resources',
    data: {
      "current": current,
      "size": size,
      "resName": dataName.toString(),
      "resType": dataType.toString(),
      "resRange": dataRange.toString(),
      "resDepartment": dataDepartment.toString(),
      "resDate": dateRange.toString(),
    }
  })
}

/**
 * {获取资源列表分页}
 * @param current
 * @param size
 * @param op
 * @returns {*}
 * @author: 宋康
 * @date: 2023/2/2
 */
export const queryResourcesPage = (current, size, op) => {
  return $axios({
    url: '/resource/item/query/page',
    method: 'POST',
    params: {
      current,
      size
    },
    data: {
      ...op
    }
  })
}

// ========================================
import {queryDictList} from "@/api/basics";

const resourceRoute = '/resource';
/**
 * {资源字典}
 *
 * @author: 宋康
 * @date: 2023/2/3
 */
export const resourceDict = {
  data() {
    return {
      serStatus: [],
      mapCsys: [],
      openMode: [],
      coverage: [],
      mapDataType: [],
      dataDataType: [],
    }
  },
  methods: {
    /**
     * {获取资源}
     *
     * @author: 宋康
     * @date: 2023/2/3
     */
    queryDict() {
      return new Promise((resolve, reject) => {
        queryDictList(['OPEN_MODE', 'COVERAGE', 'MAP_DATA_TYPE', 'DATA_DATA_TYPE', 'MAP_CSYS', 'SER_STATUS']).then(res => {
          if (res.status) {
            const data = res.data;
            this.openMode = data['OPEN_MODE'];
            this.coverage = data['COVERAGE'];
            this.mapDataType = data['MAP_DATA_TYPE'];
            this.dataDataType = data['DATA_DATA_TYPE'];
            this.mapCsys = data['MAP_CSYS'];
            this.serStatus = data['SER_STATUS'];
            resolve();
          } else {
            reject(res);
          }
        })
      })
    },
    /**
     * {范围格式化}
     *
     * @author: 宋康
     * @date: 2023/2/3
     */
    formatterCoverage({cellValue}) {
      const status = this.coverage.filter(v => v['dictKey'] === cellValue);
      if (status.length > 0) {
        return status[0]['dictValue'];
      } else {
        return '未知'
      }
    },
    /**
     * {访问权限格式化}
     *
     * @author: 宋康
     * @date: 2023/2/3
     */
    formatterOpenMode({cellValue}) {
      const status = this.openMode.filter(v => v['dictKey'] === cellValue);
      if (status.length > 0) {
        return status[0]['dictValue'];
      } else {
        return '未知'
      }
    },
    /**
     * {发布状态格式化}
     *
     * @author: 宋康
     * @date: 2023/2/8
     */
    formatterIsStartup({cellValue}) {
      if ('1' === cellValue) {
        return '在线';
      } else {
        return "离线"
      }
    },
    /**
     * {坐标系格式化}
     *
     * @author: 宋康
     * @date: 2023/2/10
     */
    formatterMapCsys({cellValue}) {
      const status = this.mapCsys.filter(v => v['dictKey'] === cellValue);
      if (status.length > 0) {
        return status[0]['dictValue'];
      } else {
        return '未知'
      }
    },
    /**
     * {服务状态格式化}
     *
     * @author: 宋康
     * @date: 2023/2/10
     */
    formatterSerStatus({cellValue}) {
      const status = this.serStatus.filter(v => v['dictKey'] === cellValue);
      if (status.length > 0) {
        return status[0]['dictValue'];
      } else {
        return '未知'
      }
    },
    /**
     * {地图数据类型格式化}
     *
     * @author: 宋康
     * @date: 2023/2/10
     */
    formatterMapDataType({cellValue}) {
      const status = this.mapDataType.filter(v => v['dictKey'] === cellValue);
      if (status.length > 0) {
        return status[0]['dictValue'];
      } else {
        return '未知'
      }
    },
  }
}

/**
 * {新建资源}
 *
 * @author: 宋康
 * @date: 2023/2/3
 */
export const saveResourceItem = (op) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/save`,
    headers: {
      "Content-Type": "multipart/form-data"
    },
    data: op
  })
}

/**
 * {更新资源}
 *
 * @author: 宋康
 * @date: 2023/2/13
 */
export const updatedResourceItem = (resourceId, field, editVal) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/updated`,
    data: {
      resourceId, field, editVal
    }
  })
}

/**
 * {获取资源列表}
 * @param current
 * @param size
 * @param op
 * @author: 宋康
 * @date: 2023/2/3
 */
export const queryResourceItemPage = (current, size, op) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/query/page`,
    params: {
      current, size
    },
    data: {
      ...op
    }
  })
}

/**
 * {获取资源信息}
 *
 * @author: 宋康
 * @date: 2023/2/8
 */
export const queryResourceItem = (resourceId) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/query/id`,
    params: {
      resourceId
    }
  })
}

/**
 * {地图资源上传}
 *
 * @author: 宋康
 * @date: 2023/2/8
 */
export const saveResourceItemMap = (op, fun) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/save/map`,
    headers: {
      "Content-Type": "multipart/form-data"
    },
    data: op,
    onUploadProgress: fun
  })
}

/**
 * {资源服务详情}
 *
 * @author: 宋康
 * @date: 2023/2/10
 */
export const queryResourceItemMap = (resourceId) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/query/server/map/id`,
    params: {
      resourceId
    }
  })
}

/**
 * {获取上传资源中文件名称}
 *
 * @author: 宋康
 * @date: 2023/2/11
 */
export const queryResourceItemMapFile = (resourceId) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/query/server/map/file/id`,
    params: {
      resourceId
    }
  })
}

/**
 * {选择后重新地图资源}
 *
 * @author: 宋康
 * @date: 2023/2/11
 */
export const updatedMap = (resourceId, name) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/updated/map`,
    params: {
      resourceId, name
    }
  })
}

/**
 * {更新地图资源信息}
 *
 * @author: 宋康
 * @date: 2023/2/13
 */
export const updatedMapInfo = (resourceId, field, editVal, editValElse) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/updated/map/info`,
    data: {
      resourceId, field, editVal, editValElse
    }
  })
}

/**
 * {上传资源API文档}
 *
 * @author: 宋康
 * @date: 2023/2/14
 */
export const uploadResourceApi = (op, fun) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/save/api`,
    headers: {
      "Content-Type": "multipart/form-data"
    },
    data: op,
    onUploadProgress: fun
  })
}

/**
 * {获取资源API文档}
 *
 * @author: 宋康
 * @date: 2023/2/14
 */
export const queryResourceApi = (resourceId) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/query/api`,
    params: {
      resourceId
    }
  })
}

/**
 * {更新资源状态}
 *
 * @author: 宋康
 * @date: 2023/2/15
 */
export const updatedResourceItemState = (resourceId, resourceState) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/updated/state`,
    data: {
      resourceId, resourceState
    }
  })
}

/**
 * {删除资源}
 *
 * @author: 宋康
 * @date: 2023/2/15
 */
export const deletedResourceItem = (resourceId) => {
  return $axios({
    method: 'POST',
    url: `${resourceRoute}/item/del`,
    params: {
      resourceId
    }
  })
}